/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.util.*;
import java.text.*;
import Models.ThamSoHT_Model;
import Models.ThongKe_Model;
import Beans.ThamSoHT;
import Models.TienTe_Model;
import Models.NhomMA_Model;
import Beans.NhomMA;
import Beans.KhuVuc;
import Models.KhuVuc_Model;
import Models.PhieuDatMA_Model;

/**
 *
 * @author Venus
 */
public class BieuDo_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("chartline")) {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat fm = new SimpleDateFormat("yyyy");
                String year = fm.format(cal.getTime());
                String dsts = "turnover";
                String turnover = ThamSoHT_Model.TenThamSoHT(dsts, langid);
                String result = "[{"
                        + " \"label\": \"" + turnover + "\",  \"value\": [  ";
                String[] arr = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
                for (int i = 0; i < arr.length; i++) {
                    double tk = ThongKe_Model.ThongKeTheoThang(Integer.parseInt(arr[i]), Integer.parseInt(year));
                    result = result + tk;
                    if (i != (arr.length - 1)) {
                        result = result + " , ";
                    }
                }
                result = result + " ] } ] ";
                out.print(result);
            } else if (action.equals("getmonth")) {
                String dsts = " 'month', 'turnover', 'monthlyturnover' ";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String month = mdsts[0].getTenTS();
                String monthlyturnover = mdsts[1].getTenTS();
                String turnover = mdsts[2].getTenTS();
                String kyhieu = TienTe_Model.LayKyHieuTT(langid);
                String s = "[{"
                        + "\"label\": \"" + month + "\","
                        + "\"value\": [\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\"]"
                        + " },{\"label\": \"" + turnover + " (" + kyhieu + ")\", \"value\" : [\"" + monthlyturnover + "\"] }] ";
                out.print(s);
            } else if (action.equals("foodchart")) {
                String dsts = "others";
                String others = ThamSoHT_Model.TenThamSoHT(dsts, langid);
                String s = "[";
                NhomMA[] nma = NhomMA_Model.DSNMA(langid);
                double percent = 0;
                double finalpercent = 0;
                for(int i =0; i< nma.length; i++){
                    percent = ThongKe_Model.ChartFoodGroup(nma[i].getMaNMA(), langid);
                    if(i == 0){
                        s = s + "{ \"name\": \""+nma[i].getTenNMA()+"\", \"y\": "+percent+", \"sliced\": true, \"selected\": true }";
                    }
                    else{
                        s = s + "{ \"name\": \""+nma[i].getTenNMA()+"\", \"y\": "+percent+", \"sliced\": false, \"selected\": false }";
                    }
                    s = s + ",";                    
                    finalpercent = finalpercent + percent;
                }
                s = s + "{ \"name\": \""+others+"\", \"y\": "+ PhieuDatMA_Model.Round(100 - finalpercent, 2) +", \"sliced\": false, \"selected\": false }";
                s = s + "]";
                out.print(s);
            }
            else if (action.equals("gettilechartfood")) {
                String dsts = "chartturnoverfood";
                String chartturnoverfood = ThamSoHT_Model.TenThamSoHT(dsts, langid);
                String s = "[{"
                        + "\"label\": \"" + chartturnoverfood + "\","
                        + "\"value\": [\"1\"]"
                        + " }] ";
                out.print(s);
            }
            else if (action.equals("gettilechartarea")) {
                String dsts = "chartturnoverarea";
                String chartturnoverarea = ThamSoHT_Model.TenThamSoHT(dsts, langid);
                String s = "[{"
                        + "\"label\": \"" + chartturnoverarea + "\","
                        + "\"value\": [\"1\"]"
                        + " }] ";
                out.print(s);
            }
            else if (action.equals("areachart")) {
                String s = "[";
                KhuVuc[] nma = KhuVuc_Model.DSKV(langid);
                double percent = 0;
                for(int i =0; i < nma.length; i++){
                    percent = ThongKe_Model.ChartArea(nma[i].getMaKV(), langid);
                    if(i == 0){
                        s = s + "{ \"name\": \""+nma[i].getTenKV()+"\", \"y\": "+percent+", \"sliced\": true, \"selected\": true }";
                    }
                    else{
                        s = s + "{ \"name\": \""+nma[i].getTenKV()+"\", \"y\": "+percent+", \"sliced\": false, \"selected\": false }";
                    }
                    if(i != (nma.length - 1)){
                        s = s + ",";
                    }
                }
                s = s + "]";
                out.print(s);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
